/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test;

import static org.junit.Assert.assertEquals;
import static org.sourceprojects.lycia.fluent.FluentBuilder.contextObject;
import static org.sourceprojects.lycia.fluent.FluentBuilder.method;
import static org.sourceprojects.lycia.fluent.FluentBuilder.nullAware;
import static org.sourceprojects.lycia.fluent.FluentBuilder.prepare;
import static org.sourceprojects.lycia.fluent.FluentBuilder.tag;
import static org.sourceprojects.lycia.fluent.FluentBuilder.validateSchema;

import org.junit.Test;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.annotations.Attribute;
import org.sourceprojects.lycia.annotations.ContextObject;
import org.sourceprojects.lycia.annotations.Nullable;
import org.sourceprojects.lycia.annotations.TextBody;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.fluent.FluentBuilder;

public class NullAwareParsingTestCase {
	private static final String XML_DATA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<matches>Test</matches>";

	@Test(expected = LyciaParsingException.class)
	public void testNullAwareFail() throws LyciaParsingException {
		final StringBuilder sb = new StringBuilder();

		final FluentBuilder<StringBuilder> builder = prepare();
		builder.configure(validateSchema(false));
		builder.parser(
				tag("matches", method("parseFail", this, StringBuilder.class,
						String.class, String.class))).configure(
				contextObject(sb), nullAware(true));

		final LyciaParser<StringBuilder> xmlParser = builder.build();

		xmlParser.parse(XML_DATA);
	}

	@Test
	public void testNullAwarePass() throws LyciaParsingException {
		final StringBuilder sb = new StringBuilder();

		final FluentBuilder<StringBuilder> builder = prepare();
		builder.configure(validateSchema(false));
		builder.parser(
				tag("matches", method("parsePass", this, StringBuilder.class,
						String.class, String.class))).configure(
				contextObject(sb));

		final LyciaParser<StringBuilder> xmlParser = builder.build();

		xmlParser.parse(XML_DATA);

		assertEquals("Test", sb.toString());
	}

	@Test
	public void testNullAwarePass2() throws LyciaParsingException {
		final StringBuilder sb = new StringBuilder();

		final FluentBuilder<StringBuilder> builder = prepare();
		builder.configure(validateSchema(false));
		builder.parser(
				tag("matches", method("parsePass", this, StringBuilder.class,
						String.class, String.class))).configure(
				contextObject(sb), nullAware(false));

		final LyciaParser<StringBuilder> xmlParser = builder.build();

		xmlParser.parse(XML_DATA);

		assertEquals("Test", sb.toString());
	}

	@Test
	public void testNullAwarePass3() throws LyciaParsingException {
		final StringBuilder sb = new StringBuilder();

		final FluentBuilder<StringBuilder> builder = prepare();
		builder.configure(validateSchema(false));
		builder.parser(
				tag("matches", method("parsePass", this, StringBuilder.class,
						String.class, String.class))).configure(
				contextObject(sb), nullAware(true));

		final LyciaParser<StringBuilder> xmlParser = builder.build();

		xmlParser.parse(XML_DATA);

		assertEquals("Test", sb.toString());
	}

	public void parseFail(@ContextObject StringBuilder sb,
			@TextBody String body, @Attribute("id") String id) {

		sb.append(body);
	}

	public void parsePass(@ContextObject StringBuilder sb,
			@TextBody String body, @Nullable @Attribute("id") String id) {

		sb.append(body);
	}

}
